package net.huashitong.ssydt.provider.article.service;


import net.huashitong.ssydt.PaginationResult;
import net.huashitong.ssydt.article.entity.SsArticleType;
import net.huashitong.ssydt.article.entity.vo.ArticleTypeInVO;
import net.huashitong.ssydt.article.entity.vo.ArticleTypeQueryInVO;
import net.huashitong.ssydt.exceptions.ParamErrorServiceException;
import net.huashitong.ssydt.exceptions.ServiceException;
import net.huashitong.ssydt.provider.article.mapper.SsArticleTypeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author <a href="mailto:yhy23456@163.com">huiyang.yu</a>
 * @since 2017.09.14
 */
@Service
@Transactional
public class SsArticleTypeServiceImpl implements SsArticleTypeService {


    private SsArticleTypeMapper mapper;

    @Autowired
    public SsArticleTypeServiceImpl(SsArticleTypeMapper mapper) {
        this.mapper = mapper;
    }

    @Override
    public int deleteByPrimaryKey(Long id) {
        return mapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(SsArticleType record) {
        return mapper.insert(record);
    }

    @Override
    public int insertSelective(SsArticleType record) {
        return mapper.insertSelective(record);
    }

    @Override
    public SsArticleType selectByPrimaryKey(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(SsArticleType record) {
        return mapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(SsArticleType record) {
        return mapper.updateByPrimaryKey(record);
    }

	@Override
	public SsArticleType getById(Long id) {
		SsArticleType ssArticleType = mapper.selectByPrimaryKey(id);
		if (ssArticleType == null) {
			throw new ServiceException("文章分类不存在");
		}
		return ssArticleType;
	}

	@Override
	public PaginationResult<SsArticleType> paginationQuery(ArticleTypeQueryInVO articleTypeQueryInVO) {
		PaginationResult result = new PaginationResult();
		result.setPageNumber(articleTypeQueryInVO.getPageNumber());
		result.setPageSize(articleTypeQueryInVO.getPageSize());

		// 查询记录总数
		int total = mapper.paginationCount(articleTypeQueryInVO);
		result.setPageRecords(total);

		if (total == 0) {
			result.setList(new ArrayList());
		} else {
			// 查询当前页记录
			List<SsArticleType> list = mapper.paginationQuery(articleTypeQueryInVO);
			result.setList(list);
		}
		return result;
	}

	@Override
	public List<SsArticleType> listValidArticleTypes() {
		return mapper.listValidArticleTypes();
	}

	@Override
	public void insert(ArticleTypeInVO articleTypeInVO) {
		validateArticleType(articleTypeInVO);

		SsArticleType ssArticleType = new SsArticleType();
		ssArticleType.setName(articleTypeInVO.getName());
		ssArticleType.setIsShow(articleTypeInVO.getIsShow());
		ssArticleType.setSequence(articleTypeInVO.getSequence());
		mapper.insert(ssArticleType);
	}

	@Override
	public void update(ArticleTypeInVO articleTypeInVO) {
		validateArticleType(articleTypeInVO);

		SsArticleType ssArticleType = mapper.selectByPrimaryKey(articleTypeInVO.getId());
		if (ssArticleType == null) {
			throw new ServiceException("文章类型不存在");
		}

		ssArticleType.setName(articleTypeInVO.getName());
		ssArticleType.setIsShow(articleTypeInVO.getIsShow());
		ssArticleType.setSequence(articleTypeInVO.getSequence());
		mapper.updateByPrimaryKeySelective(ssArticleType);
	}

	private void validateArticleType(ArticleTypeInVO articleTypeInVO) {
		switch (articleTypeInVO.getIsShow()) {
			case 0:
			case 1:
				break;
			default:
				throw new ParamErrorServiceException("是否显示输入错误");
		}
	}
}
